package com.mail.result;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.springframework.security.oauth2.common.OAuth2AccessToken;

import java.io.IOException;


public class ResponseSerializer extends StdSerializer<OauthResponse> {

    public ResponseSerializer() {
        super(OauthResponse.class);
    }

    @Override
    public void serialize(OauthResponse value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        OAuth2AccessToken oAuth2AccessToken = (OAuth2AccessToken) value.getData();

        gen.writeStartObject();
        if (oAuth2AccessToken != null){
            gen.writeStringField("code", value.getCode());
            gen.writeStringField("msg", value.getMsg());

            gen.writeObjectFieldStart("data");
            gen.writeStringField("accessToken", oAuth2AccessToken.getValue());
            gen.writeNumberField("expiresIn",oAuth2AccessToken.getExpiresIn());
            gen.writeEndObject();
        } else {
            gen.writeStringField("code", "400");
            gen.writeStringField("msg", "获取token失败");
        }

        gen.writeEndObject();
    }
}
